﻿using Microsoft.Extensions.DependencyInjection;
using Naruto.Subscribe.Interface;
using Naruto.TaskManagement.Entitys.Const;
using Naruto.TaskManagement.Entitys.DTO;
using Naruto.TaskManagement.Entitys.Models;
using Naruto.TaskManagement.Infrastructure;
using Naruto.TaskManagement.IRepository;
using Naruto.WebSocket.Extensions;
using Naruto.WebSocket.Internal;
using Naruto.WebSocket.Object;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Naruto.TaskManagement.Application.WeSocketService
{
    /// <summary>
    /// 任务管理的socket服务
    /// </summary>
    public class TaskManagementSocketService : NarutoWebSocketService
    {
        /// <summary>
        /// 发送消息
        /// </summary>
        public async Task sendMessage(SendMessageDTO info)
        {
            if (info == null || info.SendUser.IsNullEmpty() || info.SendUserId.IsNullEmpty()|| info.RecevieUser.IsNullEmpty() || info.RecevieUserId.IsNullEmpty())
            {
                return;
            }
            //创建作用域服务
            using var scopePrivider = Context.RequestServices.CreateScope();
            var unitOfWork = scopePrivider.ServiceProvider.GetRequiredService<ITaskManageUnitOfWork>();
            var chatMessageRepository = scopePrivider.ServiceProvider.GetRequiredService<IChatMessageRepository>(); ;
            //添加数据到数据库
            var addInfo = ExpressionMapper<SendMessageDTO, ChatMessage>.To(info);
            addInfo.Id = SnowFlakeHelper.NewID();
            addInfo.CreateTime = DateTime.Now;
            await chatMessageRepository.Add(addInfo);
            await unitOfWork.SaveChangeAsync();
            //发送消息给接收人
            var reciveMessageDTO = ExpressionMapper<ChatMessage, ReciveMessageDTO>.To(addInfo);
            //发送消息
            await Client.Current.SendAsync(info.RecevieUserId.ToString(), TaskManagementSocketServicesConst.ReciveMesssage, reciveMessageDTO);
            //发布消息到会话状态服务
            var publish = scopePrivider.ServiceProvider.GetRequiredService<INarutoPublish>();
            await publish.PublishAsync(ConstObject.UpdateSessionStateSubscribeKey, new UpdateChatSessionStateDTO
            {
                UserId = info.SendUserId,
                ChatContent = info.ChatContent,
                ChatContentSendUserId = info.SendUserId,
                ContentType = info.ContentType,
                ReciveUserId = info.RecevieUserId
            });
        }


        /// <summary>
        /// 更新消息为已读
        /// </summary>
        public async Task updateMessageRead(UpdateMessageReadDTO info)
        {
            if (info == null || info.id.IsNullEmpty())
            {
                return;
            }
            //创建作用域服务
            using var scopePrivider = Context.RequestServices.CreateScope();
            var unitOfWork = scopePrivider.ServiceProvider.GetRequiredService<ITaskManageUnitOfWork>();
            var chatMessageRepository = scopePrivider.ServiceProvider.GetRequiredService<IChatMessageRepository>();
            await chatMessageRepository.UpdateRead(info.id);
            await unitOfWork.SaveChangeAsync();
        }

    }
}
